/*# Capture ESXi system information.
#
# Written by: Alaa Hleihel <alaa@mellanox.com>
*/


// TODO: free memory, and organize the includes
#include "CommandList.h"
#include "vars.h"
#include "methods.h"

#include <sys/wait.h>
#include <iostream>
#include <sys/types.h>
#include <fcntl.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fstream>
#include <vector>
#include <cstring>
#include <sys/types.h> 
#include <exception>


using namespace std;

//----------------------------------------------------------------------------------------
CommandList::CommandList()
{
	count = 0;
}
// #### end of CommandList()
//----------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------
void CommandList::add(string cmd)
{
	// split the command on " "
	vector <string> splitted = strSplit( cmd, ' ');
	// add the command and it's args
	CommandWithArgs s;
	for (int ii = 0; ii < splitted.size(); ii++)
	{
		char * charr = strToCharArr((string)splitted[ii]);
        s.commandVector.push_back(charr); 
	}
	// push NULL to the end of the vector (execvp expects NULL as last element)
	s.commandVector.push_back(NULL);
	// add it to the list
	list.push_back(s);
	// update the counter
	count++;
}
// #### end of add
//----------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------
char** CommandList::getAtIndex(int index)
{
	//check input
	if (index < list.size() )
	{	
		return &(list[index].commandVector[0]);
	}
	else
	{
		return NULL;
	}
}
// #### end of getAtIndex
//----------------------------------------------------------------------------------------
